package followme.yaohuile.com.followme.network.recommend;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import followme.yaohuile.com.followme.R;

/**
 * Created by Administrator on 15-11-14.
 */
public class NetWork {
    public static void getNetData(String url,RequestCallBack<String> requestCallBack){
        HttpUtils httpUtils = new HttpUtils(5000);
        httpUtils.send(HttpRequest.HttpMethod.GET,url,requestCallBack);
    }
    public static void postNetData(String url,RequestParams params,RequestCallBack<String> callBack){
        HttpUtils httpUtils = new HttpUtils(5000);
        httpUtils.send(HttpRequest.HttpMethod.POST,url,params,callBack);
    }
    //GET请求
    public static void getData(String url, RequestCallBack<String> requestCallBack) {
        //使用HttpUtils
        HttpUtils httpUtils = new HttpUtils(5000);
        RequestParams params = new RequestParams("UTF-8");
//        if (uid != null) {
//            params.addBodyParameter("uid",uid);
//        }
        //就去进行联网请求了
        //请求回来的数据传给了requestCall
        httpUtils.send(HttpRequest.HttpMethod.GET, url, requestCallBack);
    }


    //TODO post请求
    public static void getData(String url, String client_id, String client_secret,
                               String v, String track_deviceid, String track_app_version, String track_app_channel,
                               String track_device_info, String track_os, String app_installtime,
                               String lat, String lon,
                               String page, String page_size,
                               String product_type,String continent_id,String country_id,String is_show_pay,String is_show_supplier,
                               String kw,  RequestCallBack<String> requestCallBack) {
        //使用HttpUtils
        HttpUtils httpUtils = new HttpUtils(5000);
        RequestParams params = new RequestParams("UTF-8");
        params.addBodyParameter("client_id",client_id);
        params.addBodyParameter("client_secret",client_secret);
        params.addBodyParameter("v",v);
        params.addBodyParameter("track_deviceid",track_deviceid);
        params.addBodyParameter("track_app_version",track_app_version);
        params.addBodyParameter("track_app_channel",track_app_channel);
        params.addBodyParameter("track_device_info",track_device_info);
        params.addBodyParameter("track_os",track_os);
        params.addBodyParameter("app_installtime",app_installtime);
        params.addBodyParameter("lat",lat);
        params.addBodyParameter("lon",lon);
        params.addBodyParameter("page",page);
        params.addBodyParameter("page_size",page_size);
        params.addBodyParameter("product_type",product_type);
        params.addBodyParameter("continent_id",continent_id);
        params.addBodyParameter("country_id",country_id);
        params.addBodyParameter("is_show_pay",is_show_pay);
        params.addBodyParameter("is_show_supplier",is_show_supplier);
        params.addBodyParameter("kw",kw);

//        if (uid != null) {
//            params.addBodyParameter("uid",uid);
//        }
        //就去进行联网请求了
        //请求回来的数据传给了requestCall
        httpUtils.send(HttpRequest.HttpMethod.POST, url,params, requestCallBack);
    }


    public static void getData(String url, String client_id, String client_secret,
                               String v, String track_deviceid, String track_app_version, String track_app_channel,
                               String track_device_info, String track_os, String app_installtime,
                               String lat, String lon,
                               String id,  RequestCallBack<String> requestCallBack) {
        //使用HttpUtils
        HttpUtils httpUtils = new HttpUtils(5000);
        RequestParams params = new RequestParams("UTF-8");
        params.addBodyParameter("client_id",client_id);
        params.addBodyParameter("client_secret",client_secret);
        params.addBodyParameter("v",v);
        params.addBodyParameter("track_deviceid",track_deviceid);
        params.addBodyParameter("track_app_version",track_app_version);
        params.addBodyParameter("track_app_channel",track_app_channel);
        params.addBodyParameter("track_device_info",track_device_info);
        params.addBodyParameter("track_os",track_os);
        params.addBodyParameter("app_installtime",app_installtime);
        params.addBodyParameter("lat",lat);
        params.addBodyParameter("lon",lon);

        params.addBodyParameter("id",id);

//        if (uid != null) {
//            params.addBodyParameter("uid",uid);
//        }
        //就去进行联网请求了
        //请求回来的数据传给了requestCall
        httpUtils.send(HttpRequest.HttpMethod.POST, url,params, requestCallBack);
    }

    /**
     * destination获取数据
     * @param url
     * @param requestCallBack
     */
    public static void getDataWork(String url, RequestCallBack<String> requestCallBack) {
        HttpUtils httpUtils  =new HttpUtils(5000);
        RequestParams params = new RequestParams("UTF-8");
        params.addBodyParameter("client_id","qyer_android");
        params.addBodyParameter("client_secret","9fcaae8aefc4f9ac4915");
        params.addBodyParameter("v","1");
        params.addBodyParameter("track_deviceid","860806022766611");
        params.addBodyParameter("track_app_version","5.1.1");
        params.addBodyParameter("track_app_channel","360m");
        params.addBodyParameter("track_device_info","mx2");
        params.addBodyParameter("track_os","Android4.2.1");
        params.addBodyParameter("track_user_id","");
        params.addBodyParameter("app_installtime","1404882938883");


        httpUtils.send(HttpRequest.HttpMethod.POST, url, params,requestCallBack);
    }

    /**
     * destination获取城市信息
     * @param url
     * @param requestCallBack
     */
    public static void getCityInfo(String url, RequestCallBack<String> requestCallBack) {
        HttpUtils httpUtils = new HttpUtils(5000);
        httpUtils.send(HttpRequest.HttpMethod.GET,url,requestCallBack);
    }

    public static void getCommunityList(String url, RequestCallBack<String> callBack) {

        HttpUtils httpUtils = new HttpUtils(10000);
        System.out.println("****" + url);
//        RequestParams params = new RequestParams("UTF-8");
        httpUtils.send(HttpRequest.HttpMethod.GET, url, callBack);

    }

    /**
     * 社区获取图片
     * @param imageView
     * @param icon_url
     * @param context
     */
    public static void getImage(ImageView imageView, String icon_url, Context context) {
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.configDefaultLoadFailedImage(context.getResources().getDrawable(R.mipmap.ic_qyer_gray_60));
        bitmapUtils.display(imageView, icon_url);
    }


    //    圆形图片
    public static void setCircleImage(ImageView imageView, String icon_url, Context context) {
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(imageView, icon_url, new BitmapLoadCallBack<ImageView>() {
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                Bitmap circle_img = getCricleBitmap(bitmap);
                imageView.setImageBitmap(circle_img);
            }

            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                imageView.setImageResource(R.mipmap.ic_qyer_gray_60);
            }
        });


    }


    // 设置圆形图片
    public static Bitmap getCricleBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final int color = 0xff424242;
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        int x = bitmap.getWidth();

        canvas.drawCircle(x / 2, x / 2, x / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
